<?php


namespace Littlexiaoshuishui\Upay\Gateways;


use Littlexiaoshuishui\Upay\Abstracts\GatewayInterface;
use Littlexiaoshuishui\Upay\Api;
use Littlexiaoshuishui\Upay\Gateways\Qfpay\Support;
use Littlexiaoshuishui\Upay\Gateways\Qfpay\QfpayException;

class Qfpay extends Api implements GatewayInterface
{
    public $config;

    const URL = [
        "payOrder" => '/gateway/api/v2/place.order.v2',
        "getMethod" => '/gateway/api/v2/payment.method.v2',
        "withdrawWallet" => '/gateway/transfer/wallet',
        "withdrawBank" => '/gateway/transfer/post',
    ];
    const WALLET_CODE = 'PAYTM-WITHDRAW-B2C-D-S';

    public function __construct(array $config)
    {
        $this->config = $config;
    }

    public function pay(array $param, $type=null)
    {
        $payData = [
            'channelCode'=>$param['channelCode'],
            'channelType'=>$param['channelType'],
            'payerName'=>$param['payerName'],
            'payerPhone'=>$param['payerPhone'],
            'payerEmail'=>$param['payerEmail'],
        ];
        $payData = Support::argSort($payData);
        $data = array(
            "merchantId"=>$this->config['merchantId'],
            "appId"=>$this->config['appid'],
            "currency"=>$this->config['currency'],
            "country"=>$this->config['country'],
            "orderNo"=>$param['orderNo'],
            "orderAmount"=>(string)$param['amount'],
            "goodsDescription"=>"",
            "notifyUrl"=>$this->config['notifyUrl'],
            "callBackUrl"=>$this->config['callBackUrl'],
            "payExtendParamsDtoV2"=>json_encode($payData),
            "apiVersion"=>"1.0",
        );
        $data['sign'] = Support::create($this->config)->getSign($data);
        $data["payExtendParamsDtoV2"] = $payData;
        $payUrl = $this->config['qfpayRemoteUrl'].self::URL['payOrder'];
        return Support::sendCurl($payUrl, $data);
    }

    public function find($order, $type=null)
    {
        // TODO: Implement find() method.
    }

    public function refund(array $order, $type=null)
    {
        return $this->_proxy($order,$type);
    }

    public function withdraw(array $param, $type)
    {
        if (strtolower($type) == 'wallet') {
            return $this->qfPayWithdrawWallet($param);
        }
        if (strtolower($type) == 'bank') {
            return $this->qfPayWithdrawBank($param);
        }
        throw new QfpayException("withdraw type error");

    }

    private function qfPayWithdrawWallet($param)
    {
        $data = [
            'merchantId'=>$this->config['merchantId'],
            'appId'=>$this->config['appId'],
            'orderNo'=>$param['orderNo'],
            'orderAmount'=>$param['amount'],
            'payeeAccount'=>$param['payeeAccount'],
            'walletCode'=> self::WALLET_CODE,
            'country'=>$this->config['country'],
            'currency'=>$this->config['currency'],
            'notifyUrl'=>$this->config['notifyUrl'],
        ];
        $walletUrl = $this->config['qfpayRemoteUrl'].self::URL['withdrawWallet'];
        $data['sign'] = Support::create($this->config)->getSign($data);
        return Support::sendCurl($walletUrl, $data);
    }

    private function qfPayWithdrawBank($param)
    {
        $data = [
            'merchantId'=>$this->config['merchantId'],
            'appId'=>$this->config['appid'],
            'orderNo'=>$param['orderNo'],
            'orderAmount'=>$param['amount'],
            'payeeName'=>$param['payeeName'],
            'payeeEmail'=>$param['payeeEmail'],
            'payeeMoblie'=>$param['payeeMoblie'],
            'bankCode'=>$param['bankCode'],
            'bankLinkNo'=>$param['bankLinkNo'],
            'bankName'=>$param['bankName'],
            'bankBranchName'=>$param['bankBranchName'],
            'bankCardNo'=>$param['bankCardNo'],
            'cardType'=>$param['cardType'],
            'country'=>$this->config['country'],
            'currency'=>$this->config['currency'],
            'notifyUrl'=>$param['notifyUrl'],
        ];
        $data['sign'] = Support::create($this->config)->getSign($data);
        $bankUrl = $this->config['qfpayRemoteUrl'].self::URL['withdrawBank'];
        return Support::sendCurl($bankUrl, $data);
    }

    public function verify($data)
    {
        $sign = $data['sign'];
        unset($data['sign']);
        return Support::create($this->config)->verify($data, $sign);
    }

    public function getMethods()
    {
        $params = array(
            "merchantId"=>$this->config['merchantId'],
            "appId"=>$this->config['appid'],
            "currency"=>$this->config['currency'],
            "country"=>$this->config['country'],
            "apiVersion"=>"1.0",
        );
        $params["sign"] = Support::create($this->config)->getSign($params);
        $getMethodUrl = $this->config['qfpayRemoteUrl'].self::URL['getMethod'];
        return Support::sendCurl($getMethodUrl, $params);
    }

}